package com.mingqijia.gassafety.authority.vcc.oauth;

import com.gap.basic.base.CommonResponse;
import com.mingqijia.gassafety.authority.vcc.provider.VccOauthProvider;
import com.mingqijia.gassafety.authority.vcc.user.VccUserLoginHolder;
import com.mingqijia.gassafety.authority.vcc.valueobject.VccUserVO;
import com.mingqijia.gassafety.shared.utils.ApplicationUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 自定义退出处理类 返回成功
 * @author Damon S.
 * @version v1.0.1
 * @date 2020年08月04日 22:32
 */
@Configuration
@RequiredArgsConstructor
public class VccLogoutSuccessHandlerImpl implements LogoutSuccessHandler {

    private final VccUserLoginHolder holder;
    private final VccOauthProvider provider;

    @Override
    public void onLogoutSuccess(HttpServletRequest request,
                                HttpServletResponse response,
                                Authentication auth) throws IOException, ServletException {
        VccUserVO backend = (VccUserVO) auth.getPrincipal();
        holder.whenManagerLogout(backend);

        // 设置返回信息
        ApplicationUtils.writeOutJson(response, CommonResponse.success(provider.logout(request)));
    }
}
